package com.example.tkserver.dao;

import com.example.tkserver.entity.Answer;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface AnswerDao {

    @Select("SELECT COUNT(qid) from answers WHERE qid = #{qid}")
    public int getAnswerNum(int qid);

    @Select("SELECT " +
            "u.uid,u.avatarUrl,u.nickName,unix_timestamp(a.answerTime) datetime,a.*" +
            "FROM user u,answers a " +
            "WHERE qid = #{qid} AND a.uid = u.uid")
    public List<Answer> getAnswerBetween(int qid,int start,int end);

    @Insert("INSERT INTO answers(qid,uid,content,endorse,answerTime) " +
            "VALUES(#{qid},#{uid},#{content},#{endorse},FROM_UNIXTIME(#{datetime},'%Y-%m-%d %h:%i:%s'))")
    @Options(useGeneratedKeys = true, keyProperty="aid", keyColumn="aid")
    public int publishAnswer(Answer answer);
}
